<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="clickAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default=" { row }">
              <el-button v-if="row.type === 1" size="small" type="text" @click="clickAdd(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="clickEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="showTitle" @close="closeDialog">
        <!-- 表单内容 -->
        <el-form v-loading="loading" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />

          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />

          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />

          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqGetPermissionList, reqUpdatePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      loading: false
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑' : '添加'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      const treeData = tranListToTreeData(data, '0')
      this.list = treeData
    },
    del(id) {
      this.$confirm('亲亲，你确定要进行删除吗？该操作不可逆哦！！', '警告').then(async() => {
        await reqDelPermission(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      }).catch(() => {
        console.log('取消')
      })
    },
    clickAdd(type, pid) {
      // 弹框
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },

    async clickEdit(id) {
      // 1 显示弹框
      this.showDialog = true
      this.loading = true
      // 2 发送请求，获取原有权限信息数据，进行回显
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data

      this.loading = false
    },

    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下

      }
    },
    async  clickSubmit() {
      if (this.formData.name.trim() === '') {
        this.$message.error('请输入权限名称')
        return
      }
      if (this.formData.code.trim() === '') {
        this.$message.error('请输入权限标识')
        return
      }

      if (this.formData.id) {
        // 进行编辑操作
        await reqUpdatePermission(this.formData)
        this.$message.success('编辑成功')
      } else {
        // 进行添加操作
        await reqAddPermission(this.formData)
        this.$message.success('添加成功')
      }
      // // 通过校验，发送提交请求
      // await reqAddPermission(this.formData)
      this.closeDialog()
      this.getPermissionList()
      // this.$message.success('添加成功')
    }
  }
}
</script>
